/*
 moxl - Modular XML Library
 Copyright (c) 2007, Malcolm Sparks

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package moxl.dom.ethereal;

import java.util.Iterator;
import java.util.List;

import moxl.dom.MoxlCharacterData;
import moxl.dom.MoxlElement;
import moxl.dom.MoxlNode;
import moxl.dom.support.MoxlNodeList;

class EtherealElementChildren implements Iterable<MoxlNode>, org.w3c.dom.NodeList
{
	private final MoxlNodeList nodes;

	public EtherealElementChildren(MoxlElement parent)
	{
		if (parent == null)
		{
			throw new IllegalArgumentException("parent cannot be null");
		}
		nodes = new MoxlNodeList(parent);
	}

	public void append(MoxlNode child)
	{
		nodes.append(child);
	}

	public int size()
	{
		return nodes.size();
	}

	public void setContent(String content)
	{
		if (!nodes.isEmpty())
		{
			throw new IllegalStateException("Cannot set content if element already contains child nodes");
		}
		nodes.append(new EtherealCharacterData(content));
	}

	public String getContent()
	{
		StringBuilder builder = new StringBuilder();
		for (MoxlNode node : nodes)
		{
			if (node instanceof MoxlCharacterData)
			{
				MoxlCharacterData cdata = (MoxlCharacterData) node;
				builder.append(cdata.getData());
			}
			else
			{
				throw new IllegalStateException("Element is mixed");
			}
		}
		return builder.toString();
	}

	public Iterator<MoxlNode> iterator()
	{
		return nodes.iterator();
	}

	public boolean isEmpty()
	{
		return nodes.isEmpty();
	}

	public boolean containsCharacterData()
	{
		for (MoxlNode node : nodes)
		{
			if (node instanceof MoxlCharacterData)
			{
				return true;
			}
		}
		return false;
	}

	public boolean containsCharacterDataExcludingWhitespace()
	{
		for (MoxlNode node : nodes)
		{
			if (node instanceof MoxlCharacterData)
			{
				MoxlCharacterData cdata = (MoxlCharacterData) node;
				if (!cdata.isWhitespace())
				{
					return true;
				}
			}
		}
		return false;
	}

	public int countChildElements()
	{
		return nodes.countElements();
	}

	public List<MoxlElement> getChildElements()
	{
		return nodes.getElements();
	}

	public List<MoxlNode> getChildNodes()
	{
		return nodes.getNodes();
	}

	public MoxlNode getChildNode(int index)
	{
		return nodes.getNodeAtIndex(index);
	}

	public MoxlElement getChildElement(int index)
	{
		List<MoxlElement> childElements = getChildElements();
		if (index >= childElements.size())
		{
			return null;
		}
		return childElements.get(index);
	}

	public void insert(MoxlNode node)
    {
	    nodes.insert(node);
    }

	public int getLength()
    {
	    throw new UnsupportedOperationException("TODO");
    }

	public org.w3c.dom.Node item(int index)
    {
	    throw new UnsupportedOperationException("TODO");
    }

}
